import shutil
from fnmatch import fnmatch, fnmatchcase
def init_global_env(use_oneai=False, install_modules=[],copy_dirs=None,copy_files=None, clean_cache=False,randomFolder=False):
    import logging
    logger = logging.getLogger()
    import os
    import time
    print("init_global_env: use_oneai={},init_intall={},copy_dirs={},copy_files={}".format(use_oneai,install_modules,copy_dirs,copy_files))

    if use_oneai:
        import moxing as mox
        mox.file.shift('os', 'mox')

        print('running init environment')
        if install_modules:
            import oneai_util
            OBS_ACCESS_KEY = os.environ.get('OBS_AK', '')
            OBS_SECURITY_KEY = os.environ.get('OBS_SK', '')
            OBS_SERVER = 'obs.cn-north-2.myhwclouds.com'
            oneai_util.init_env(obs_ak=OBS_ACCESS_KEY, obs_sk=OBS_SECURITY_KEY, obs_server=OBS_SERVER,install_modules=install_modules)

        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
        print("Start")
        start_time = time.time()
        # Training settings
        import argparse
        parser = argparse.ArgumentParser(description='BERT')
        parser.add_argument('--data_url', type=str, default='', metavar='OBS_INPUT',
                            help='创建作业时用户选择的数据集OBS的URL, obs://bucket/madataset OR hdfs:// OR file:/')
        parser.add_argument('--train_url', type=str, default='', metavar='OBS_OUTPUT',
                            help='模型输出到OBS上的路径, oneAI会自动生成，自己指定路径无效')
        parser.add_argument('--num_gpus', type=int, default=1, metavar='GPU_NUM')
        parser.add_argument('--world_size', type=int, default=2, help='Total number of nodes')
        parser.add_argument('--init_method', type=str, default=None, help='master address')
        parser.add_argument('--rank', type=int, default=0, help='Index of current task')



        args_oneai, unknown = parser.parse_known_args()

        logger.info('Total cost time:%s' % (time.time() - start_time))

       # base_dir = 's3://obs-app-2019061222072901611/l00470783/BERT_NER/' #AI服务
       # base_dir = 's3//obs-app-2020022809505006571/l00470783/BERT_NER/'  #内容审核
        base_dir = args_oneai.data_url

        logger.info("Project base_dir = {}".format(base_dir))
        print('Project base_dir is {}'.format(base_dir))
        print('data.url is {}'.format(args_oneai.data_url))

        # 拷贝文件
        redirect_dir = '/cache/j00450206'
        if randomFolder:
            redirect_dir += "/" + time.strftime("%Y%m%d%H%M%S", time.localtime())  # make sure not ending with backslash
        if clean_cache:
            shutil.rmtree(redirect_dir)
        os.makedirs(redirect_dir, exist_ok=True)
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'copy project file')
        print("current directories in redirect_dir before copy")
        print("redirect_dir:"+redirect_dir)
        print(os.listdir(redirect_dir))
        time1= time.time()
        if copy_dirs:
            print("starting copy_dir")
            print(copy_dirs)
            for copy_dir in copy_dirs:
                mox.file.copy_parallel(base_dir+"/" + copy_dir, redirect_dir + "/" + copy_dir ) #src_dir 所有的文件夹和文件都会被复制到target dir 下面，成为target文件夹的子文件夹和文件, 如果target文件夹不存在，会新建一个target文件夹
        if copy_files:
            print("starting copy_file")
            print(copy_files)
            if isinstance(copy_files,str):
                copy_files = [copy_files]
            for copy_file in copy_files:
                copy_file_dir = os.path.dirname(copy_file)
                copy_file_name = os.path.basename(copy_file)
                if "*" in copy_file:
                    real_files = mox.file.list_directory(base_dir + "/" + copy_file_dir,recursive=False)
                    real_files = [ os.path.basename(x) for x in real_files if fnmatch(x,copy_file_name)]
                else:
                    real_files = [ os.path.basename(copy_file)]
                for real_file in real_files:
                    mox.file.copy(base_dir + "/" + copy_file_dir + "/" + real_file,
                                  redirect_dir + "/" + copy_file_dir + "/" + real_file)  # s

        #else:
        #    mox.file.copy_parallel(base_dir, redirect_dir)
        tim2 = time.time()
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 'done copy project files')
        print("current directories in redirect_dir after copy")
        print(os.listdir(redirect_dir))

        print("total time elapsed copying file:{} minutes".format((tim2-time1)/60))


        base_dir = redirect_dir

        print("current base_dir is {}".format(base_dir))

        # base_dir = '/home/work/' # sometimes does not work
    else:
        base_dir = os.path.abspath('..')
        args_oneai = None

    return base_dir, args_oneai



